export type DateType = {
    title: string
    value: number
    year: number
    month: number,
    timestamp: number
}

export const getTodayStartTimestamp = (): number => {
    const now = new Date();
    const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    return todayStart.getTime();
}

export const getDaysInMonth = (year: number, month: number): number => {
    // JavaScript 中月份从 0 开始，所以需要将传入的月份减 1
    return new Date(year, month, 0).getDate();
}

export const getFirstDayOfMonth = (year: number, month: number): number => {
    // JavaScript 中月份从 0 开始，所以需要将传入的月份减 1
    return new Date(year, month - 1, 1).getDay();
}

export const getPreviousMonthDays = (year: number, month: number): DateType[] => {
    const previousMonth = month - 1 < 1 ? 12 : month - 1;
    const previousYear = month - 1 < 1 ? year - 1 : year;
    const daysInPreviousMonth = getDaysInMonth(previousYear, previousMonth);
    const firstDayOfCurrentMonth = getFirstDayOfMonth(year, month);

    const previousMonthDays: DateType[] = [];
    for (let i = 0; i < firstDayOfCurrentMonth - 1; i++) {
        previousMonthDays.unshift({
            title: (daysInPreviousMonth - i).toString(),
            value: daysInPreviousMonth - i,
            year:previousYear,
            month:previousMonth,
            timestamp: new Date(`${previousYear}-${previousMonth}-${daysInPreviousMonth - i} 00:00:00`).getTime()
        });
    }

    return previousMonthDays;
}

export
const getNextMonthDays = (year: number, month: number, calendarLength: number): DateType[] => {
    const nextMonth = month + 1 > 12 ? 1 : month + 1;
    const nextYear = month + 1 > 12 ? year + 1 : year;

    let nextMonthDays: DateType[] = [];
    for (let i = 1; calendarLength + nextMonthDays.length < 42; i++) {
        nextMonthDays.push({
            title: i.toString(),
            value: i,
            year:nextYear,
            month:nextMonth,
            timestamp: new Date(`${nextYear}-${nextMonth}-${i} 00:00:00`).getTime()
        });
    }
    nextMonthDays = nextMonthDays.slice(0,nextMonthDays.length - 7)

    return nextMonthDays;
}

export const generateCalendar = (year: number, month: number, isNextYear:boolean = true): DateType[] => {
    const daysInMonth = getDaysInMonth(year, month);
    const previousMonthDays = getPreviousMonthDays(year, month);
    const nextMonthDays = getNextMonthDays(year, month, previousMonthDays.length + daysInMonth);

    // 填充日历数组
    const calendar: DateType[] = [];
    let day = 1;

    // 先填充上一月的日期
    calendar.push(...previousMonthDays);

    // 填充当前月的日期
    while (day <= daysInMonth) {
        calendar.push({
            title: day.toString(),
            value: day,
            year:year,
            month:month,
            timestamp: new Date(`${year}-${month}-${day} 00:00:00`).getTime()
        });
        day++;
    }

    if(month == 12 && isNextYear || month !== 12){
        // 填充下一月的日期
        calendar.push(...nextMonthDays);
    }
    return calendar;
}
